﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace UIV1.UC
{
    public partial class AddProduct : UserControl
    {
        Timer _cbbProductNameTimer;
        int _cbbProductNameFlag;
        Thread cbbProductNameThread;
        object _cbbProductNameObject = new object();


        public AddProduct()
        {
            InitializeComponent();
            Load += new EventHandler(AddProduct_Load);
        }

        void AddProduct_Load(object sender, EventArgs e)
        {
            tableLayoutPanel1.SetRowSpan(panel4, 3);
            tableLayoutPanel2.SetRowSpan(panel8, 2);
            tableLayoutPanel3.SetRowSpan(panel11, 2);
            cbbMaterialUnitOfWeight.SelectedIndex = 1;
            cbbProductUnitOfWeight.SelectedIndex = 1;
            cbbProductName.TextChanged += new EventHandler(cbbProductName_TextChanged);
            if (_cbbProductNameTimer == null)
            {
                _cbbProductNameTimer = new Timer();
                _cbbProductNameTimer.Interval = 1000;
                _cbbProductNameTimer.Tick += new EventHandler(_cbbProductNameTimer_Tick);
            }
        }

        void _cbbProductNameTimer_Tick(object sender, EventArgs e)
        {
            lock (_cbbProductNameObject)
            {
                _cbbProductNameFlag++;
                if (_cbbProductNameFlag == 1)
                {
                    _cbbProductNameTimer.Stop();
                    _cbbProductNameFlag = 0;
                    //Add item
                    cbbProductName.Items.Clear();
                    cbbProductName.Items.AddRange(new string[3] { "Item1", "Item2", "Item3" });
                    cbbProductName.DropDownStyle = ComboBoxStyle.DropDown;
                    cbbProductName.DroppedDown = true;
                }
            }
        }

        void cbbProductName_TextChanged(object sender, EventArgs e)
        {
            lock (_cbbProductNameObject)
            {
                _cbbProductNameFlag = 0;
                _cbbProductNameTimer.Stop();
            }

            if (!string.IsNullOrEmpty(cbbProductName.Text))
            {
                _cbbProductNameTimer.Start();
            }
            else
            {
                cbbProductName.DropDownStyle = ComboBoxStyle.Simple;
            }
        }
    }
}
